package com.fun_ordering.service.impl;

import com.fun_ordering.service.FoEmployeeService;
import com.fun_ordering.vo.UpdatePasVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.mapper.FoEmployeeMapper;
import com.fun_ordering.pojo.FoEmployee;
import com.fun_ordering.pojo.FoEmployeeExample;
import com.fun_ordering.pojo.FoEmployeeExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 员工表服务实现层
 * @author XiaoPi
 *
 */
@Service
public class FoEmployeeServiceImpl implements FoEmployeeService {

	@Autowired
	private FoEmployeeMapper foEmployeeMapper;

	
	/**
	 * 查询全部
	 */
	@Override
	public List<FoEmployee> findAll() {
		return foEmployeeMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<FoEmployee> page=   (Page<FoEmployee>) foEmployeeMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(FoEmployee foEmployee) {
		foEmployeeMapper.insert(foEmployee);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(FoEmployee foEmployee){
		foEmployeeMapper.updateByPrimaryKey(foEmployee);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param empId
	 * @return
	 */
	@Override
	public FoEmployee findOne(Integer empId){
		return foEmployeeMapper.selectByPrimaryKey(empId);
	}

    @Override
    public FoEmployee selectByOne(String empAccount) {
        return foEmployeeMapper.selectByOne(empAccount);
    }

    /**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] empIds) {
		for(Integer empId:empIds){
			foEmployeeMapper.deleteByPrimaryKey(empId);
		}		
	}
	
	
		@Override
	public PageResult findPage(FoEmployee foEmployee, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		FoEmployeeExample example=new FoEmployeeExample();
		Criteria criteria = example.createCriteria();
		
		if(foEmployee!=null){			
						if(foEmployee.getEmpName()!=null && foEmployee.getEmpName().length()>0){
				criteria.andEmpNameLike("%"+foEmployee.getEmpName()+"%");
			}			if(foEmployee.getPhoneNumber()!=null && foEmployee.getPhoneNumber().length()>0){
				criteria.andPhoneNumberLike("%"+foEmployee.getPhoneNumber()+"%");
			}			if(foEmployee.getEmpAccount()!=null && foEmployee.getEmpAccount().length()>0){
				criteria.andEmpAccountLike("%"+foEmployee.getEmpAccount()+"%");
			}			if(foEmployee.getEmpPassword()!=null && foEmployee.getEmpPassword().length()>0){
				criteria.andEmpPasswordLike("%"+foEmployee.getEmpPassword()+"%");
			}	
		}
		
		Page<FoEmployee> page= (Page<FoEmployee>)foEmployeeMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}
	/**
	 *作用：修改密码
	 *方法名：
	 *参数: [newPas, oldPas]
	 *返回值: int
	 */
	@Override
	public void updatePas(UpdatePasVO updatePasVO) {

		foEmployeeMapper.updatePas(updatePasVO);
	}

}
